﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using ConexionAdmin;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Collections;

namespace Manager
{
    class AdminController
    {
        public AdminController()
        {
        }

        public void IniciarServicio()
        {
            Thread th = new Thread(AtenderAdmin);
            th.Start();
            
        }

        private void AtenderAdmin()
        {
            IServicioAdmin servicioRemoto = new ServicioAdmin();
            BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
            serverProv.TypeFilterLevel = TypeFilterLevel.Full;
            //RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
            serverProv.TypeFilterLevel = TypeFilterLevel.Full;

            IDictionary propBag = new Hashtable();
            propBag["port"] = Manager.conf.Puerto_administrador;
            propBag["typeFilterLevel"] = TypeFilterLevel.Full;
            propBag["name"] = "AdminChannel";  
            TcpChannel tcpChan = new TcpChannel(propBag, null, serverProv);
            ChannelServices.RegisterChannel(tcpChan, false); 

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServicioAdmin), "Admin", WellKnownObjectMode.Singleton);

            Console.WriteLine("Escuchando peticiones del administrador (" + Manager.conf.Ip_administrador + ") en el puerto " + Manager.conf.Puerto_administrador + "..");
        }
    }
}
